package com.ingenio.siot.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import com.ingenio.siot.Helper;
import com.ingenio.siot.adapter.CriterioAdapter;
import com.ingenio.siot.adapter.CriterioIntervaloAdapter;
import com.ingenio.siot.entity.Criterio;
import com.ingenio.siot.entity.CriterioIntervalo;

/**
 * Servlet implementation class DevolverCriterioServlet
 */
@WebServlet("/DevolverCriterio")
public class DevolverCriterioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DevolverCriterioServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			JSONObject json = new JSONObject();
			JSONArray ja = new JSONArray();
			if(request.getParameter("id") != null && Helper.isInteger(request.getParameter("id").trim())){
				Integer id = Integer.parseInt(request.getParameter("id").trim());
				CriterioAdapter ca = new CriterioAdapter();
			    Criterio c = ca.devolverCriterio(id);
			    CriterioIntervaloAdapter cia = new CriterioIntervaloAdapter();
			    List<CriterioIntervalo> intervalosCriterio = cia.devolverIntervalosDelCriterio(c);
				if(c != null){
					json.put("descripcion", c.getDescCriterio());
					json.put("formula", c.getFormula());
					json.put("criterioPublico", c.isCriterioPublico());
					for (CriterioIntervalo ci : intervalosCriterio) {
						JSONObject int_criterioJSON = new JSONObject();
						JSONObject tipo = new JSONObject();
						JSONObject estado = new JSONObject();
						estado.put("id", ci.getEstado().getIdEstado());
						estado.put("descripcion", ci.getEstado().getDescEstado());
						int_criterioJSON.put("estado", estado);
						int_criterioJSON.put("max", ci.getValorMaximo());
						int_criterioJSON.put("min", ci.getValorMinimo());
						tipo.put("id", ci.getTipoIntervalo().getIdTipoIntervalo());
						tipo.put("condicion", ci.getTipoIntervalo().getCondicion());
						tipo.put("descripcion", ci.getTipoIntervalo().getDescTipoIntervalo());
						int_criterioJSON.put("tipo_intervalo", tipo);
						ja.put(int_criterioJSON);
					}
					json.put("criterios", ja);
				}else{
					json.put("error", "No recupera criterio");
				}
			}else{
				json.put("error", "error id");
			}
			response.setContentType("application/json");
			response.getWriter().write(json.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
